#ifndef _FRAMEBUFFER
#define _FRAMEBUFFER

#include "Base.h"

#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;

class ColorRGBA
{
public:
  
  union
  {
    struct { fp r,g,b,a; };
    struct { fp c[4]; };
  };

  ColorRGBA(
    const fp _r=0.0f,
    const fp _g=0.0f,
    const fp _b=0.0f,
    const fp _a=1.0f
  );
  ColorRGBA(const ColorRGBA& _c) { Set(_c); };
  
  void Clear() { r=g=b=0.0f; a=1.0f; }
  void Set(const ColorRGBA& _c);
  ui8  GetColorRGB8(ui32 _c, const fp gamma = 2.2f) const;
};

typedef void (*Callback)(const ui32 x, const ui32 y, ColorRGBA& c);

class RGBA
{
public:

  vector<ColorRGBA> data;
  ui32 w, h;
  Callback cb;

  RGBA(const ui32 _w=1280, const ui32 _h=720);
  ~RGBA() { data.clear(); }

  bool IsValid() const;
  void Clear();
};

class Framebuffer
{
public:
  
  vector<RGBA> stack;

  Framebuffer() { stack.clear(); };
  ~Framebuffer() { stack.clear(); };

  i32  Create  (const ui32 w=1280, const ui32 h=720);
  void Register(const ui32 slot = 0, const Callback cb = NULL);
  void Process (const ui32 slot = 0);
  void Write   (
    const ui32 slot = 0,
    string title = "undefined",
    const fp gamma = 2.2f
  ) const;
};

#endif // _FRAMEBUFFER
